package com.mk.settlement.controller;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.mk.settlement.kafka.SettlementKafkaConsumer;
import com.mk.settlement.model.Order;

@Controller
@RequestMapping("/sc/order")
public class SettlementOrderController {
	
    private Logger log = LoggerFactory.getLogger(SettlementOrderController.class);

    @Resource
    private SettlementKafkaConsumer settlementKafkaConsumer;
	
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
	public String insertBaseOrder(long hotelid, String hotelname, BigDecimal hprice ,Long otaid, String otaname, 
			BigDecimal oprice , Long orderid, int paytype, String checkouttime, int saletype){
    	
    	log.info("插入订单结算基础数据开始。");
    	log.info("输入参数:hotelid=>" + hotelid + ",hotelname=>" + hotelname + ",hprice=>" + hprice + ",otaid=>" + otaid + 
    			",otaname=>" + otaname + ",oprice=>" + oprice + ",orderid=>" + orderid + ",paytype=>" + paytype + 
    			",checkouttime=>" + checkouttime + ",saletype=>" + saletype);
    	
		Order order = new Order();
			order.setHotelid(hotelid);
			order.setHotelname(hotelname);
			order.setHprice(hprice);
			
			order.setOtaid(otaid);
			order.setOtaname(otaname);
			order.setOprice(oprice);
			
			order.setOrderid(orderid);
			order.setPayType(paytype);
			
			SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
			try {
				Date d = sim.parse(checkouttime + " 00:00:00");
				order.setCheckouttime(d);
			} catch (ParseException e1) {
				e1.printStackTrace();
			}
			order.setSaletype(saletype);
		String orderJson = JSON.toJSONString(order);
		
		try {
			settlementKafkaConsumer.orderConsume(orderJson);
			log.info("插入订单结算基础数据完成。");
			return "添加成功";
		} catch (Exception e) {
			log.info("插入数据异常。");
			throw e;
		}
	}

}
